﻿using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using Shpm.App.Input.Bean;
using Shpm.App.Input.Dao;
using Shpm.App.Master.Bean;
using Shpm.App.Master.Dao;
using Shpm.App.Commons.Db.Beans.Master;
using Shpm.App.Commons.Utils;
using Shpm.System;

public partial class App_Input_YigongUpdForm : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
        {
            this.ViewState["YIGONG_ID"] = this.Context.Items["YIGONG_ID"];
            initDic();
            this.rblSex.Items.RemoveAt(0);
            if (rblSex.Items.Count > 0)
            {
                rblSex.SelectedIndex = 0;
            }
        }

        //传递检索参数与页面编号
        if (Context.Items.Contains("YIGONG_NAME"))
        {
            this.ViewState["YigongName"] = this.Context.Items["YIGONG_NAME"];
        }
        if (Context.Items.Contains("SEX"))
        {
            this.ViewState["Sex"] = this.Context.Items["SEX"];
        }
        if (Context.Items.Contains("SEND_DATE"))
        {
            this.ViewState["SendDate"] = this.Context.Items["SEND_DATE"];
        }
        if (Context.Items.Contains("SEND_DATE_TO"))
        {
            this.ViewState["SendDateTo"] = this.Context.Items["SEND_DATE_TO"];
        }
        if (Context.Items.Contains("ParamPageIndex"))
        {
            this.ViewState["ParamPageIndex"] = this.Context.Items["ParamPageIndex"];
        }
    }

    /*
     * 函数名：initDic
     * 功能：控件数据绑定
     * 参数：无
     * 说明：将数据字典中的数据绑定在指定的控件上
     */
    private void initDic()
    {

        CommonDictionaryBean paramBean = new CommonDictionaryBean();
        CommonDictionaryDao dao = new CommonDictionaryDao();
        /*籍贯*/
        paramBean.Catalog = "M_PERSON.NATIVE";
        initAll(dao, paramBean, this.drpNative);
        //性别
        paramBean.Catalog = "SEX";
        initAll(dao, paramBean, this.rblSex);
    }

    /*
    * 函数名：initAll
    * 功能  ：控件数据绑定
    * 参数  ：dao(数据字典的DAO）
    *       ：paramBean(数据字典的Bean）
    *       ：obj（控件）
    * 说明  ：
    */
    private void initAll(CommonDictionaryDao dao, CommonDictionaryBean paramBean, ListControl obj)
    {
        IList list = null;
        list = dao.FindByCatalog(paramBean);

        obj.DataSource = list;
        obj.DataBind();
    }

    protected override void OnPreRender(EventArgs e)
    {
        if (this.ViewState["YIGONG_ID"] != null)
        {
            loadDate();
        }
    }

    /*
     * 函数名：loadDate
     * 功能：数据读取
     * 参数：无
     * 说明：
     */
    private void loadDate()
    {
        YigongBean dataBean = new YigongBean();
        YigongDao Dao = new YigongDao();

        dataBean.YigongId = Convert.ToInt32(this.ViewState["YIGONG_ID"]);
        dataBean = Dao.GetYigong(dataBean);
        if (dataBean != null)
        {
            txtNameC.Text = dataBean.NameC;
            txtNameE.Text = dataBean.NameE;
            rblSex.SelectedValue = (this.rblSex.Items.FindByValue(dataBean.Sex) == null) ? null : dataBean.Sex;
            txtBirthday.Text = dataBean.Birthday == null ? "" : DateUtils.convertToString((DateTime)dataBean.Birthday);
            drpNative.SelectedValue = (this.drpNative.Items.FindByValue(dataBean.Native) == null) ? null : dataBean.Native;
            txtJob.Text = dataBean.Job;
            txtCompany.Text = dataBean.Company;
            txtTelCompany.Text = dataBean.TelCompany;
            txtTelHome.Text = dataBean.TelHome;
            txtHandSet.Text = dataBean.HandSet;
            txtEmail.Text = dataBean.Email;
            txtXueli.Text = dataBean.Xueli;
            txtSpeciality.Text = dataBean.Speciality;
            txtMusic.Text = dataBean.Music;
            txtSports.Text = dataBean.Sports;
            txtDrama.Text = dataBean.Drama;
            txtDance.Text = dataBean.Dance;
            txtLanguage.Text = dataBean.Language;
            chkIkebana.Checked = dataBean.Ikebana;
            chkLiterrature.Checked = dataBean.Literrature;
            chkPhotoGraphy.Checked = dataBean.Photography;
            txtOtherSkill.Text = dataBean.OtherSkills;
            chkService.Checked = dataBean.Service;
            chkEduCate.Checked = dataBean.Educate;
            chkSocial.Checked = dataBean.Social;
            txtOtherService.Text = dataBean.OtherService;
            txtFax.Text = dataBean.Fax;
        }
    }
    protected void btnAdd_Click(object sender, EventArgs e)
    {
        if (this.ComValidator1.DoValidator() == false)
        {
            ((Design_MasterPage)this.Master).InfoMessage = this.ComValidator1.ErrorMessage;

            return;
        }
        SysUserManager manager = new SysUserManager(Context);
        string userId = manager.getSysUserBean().getMstSysUserBean().SysUserId;

        YigongBean dataBean = new YigongBean();
        YigongDao Dao = new YigongDao();

        dataBean.NameC = txtNameC.Text;
        dataBean.NameE = txtNameE.Text;
        dataBean.Sex = rblSex.SelectedValue;
        dataBean.Birthday = DateUtils.convertToDate(txtBirthday.Text);
        dataBean.Native = drpNative.SelectedValue;
        dataBean.Job = txtJob.Text;
        dataBean.Company = txtCompany.Text;
        dataBean.TelCompany = txtTelCompany.Text;
        dataBean.TelHome = txtTelHome.Text;
        dataBean.HandSet = txtHandSet.Text;
        dataBean.Email = txtEmail.Text;
        dataBean.Xueli = txtXueli.Text;
        dataBean.Speciality = txtSpeciality.Text;
        dataBean.Music = txtMusic.Text;
        dataBean.Sports = txtSports.Text;
        dataBean.Drama = txtDrama.Text;
        dataBean.Dance = txtDance.Text;
        dataBean.Language = txtLanguage.Text;
        dataBean.Ikebana = chkIkebana.Checked;
        dataBean.Literrature = chkLiterrature.Checked;
        dataBean.Photography = chkPhotoGraphy.Checked;
        dataBean.OtherSkills = txtOtherSkill.Text;
        dataBean.Service = chkService.Checked;
        dataBean.Educate = chkEduCate.Checked;
        dataBean.Social = chkSocial.Checked;
        dataBean.OtherService = txtOtherService.Text;
        dataBean.Fax = txtFax.Text;
        dataBean.UserId = userId;
        //数据更新
        if (this.ViewState["YIGONG_ID"] != null)
        {
            dataBean.YigongId = Convert.ToInt32(ViewState["YIGONG_ID"]);

            Dao.UpdYigong(dataBean);
            Response.Write("<script language=JavaScript>" +
                      "alert('" + HttpContext.GetGlobalResourceObject(Constants.GLOBAL_RESOURCE_NAME, "I0002").ToString() + "');</script>");

            string url = "~/App/Master/YigongSrhForm.aspx";

            //返回
            Context.Items["YIGONG_NAME"] = Convert.ToString(ViewState["YigongName"]);
            Context.Items["SEX"] = Convert.ToString(ViewState["Sex"]);
            Context.Items["SEND_DATE"] = Convert.ToString(ViewState["SendDate"]);
            Context.Items["SEND_DATE_TO"] = Convert.ToString(ViewState["SendDateTo"]);
            Context.Items["ParamPageIndex"] = Convert.ToString(ViewState["ParamPageIndex"]);
            ControllerUtils.Transfer(Context, url, true);
        }
        //数据新规

        else
        {
            this.ViewState["YIGONG_ID"] = (int)Dao.InsertYigong(dataBean);
            Response.Write("<script language=JavaScript>" +
            "alert('" + HttpContext.GetGlobalResourceObject(Constants.GLOBAL_RESOURCE_NAME, "I0001").ToString() + "');</script>");

            string url = "~/App/Master/YigongSrhForm.aspx";

            //返回
            Context.Items["YIGONG_NAME"] = Convert.ToString(ViewState["YigongName"]);
            Context.Items["SEX"] = Convert.ToString(ViewState["Sex"]);
            Context.Items["SEND_DATE"] = Convert.ToString(ViewState["SendDate"]);
            Context.Items["SEND_DATE_TO"] = Convert.ToString(ViewState["SendDateTo"]);
            Context.Items["ParamPageIndex"] = Convert.ToString(ViewState["ParamPageIndex"]);
            ControllerUtils.Transfer(Context, url, true);
        }

    }
    protected void btnBack_Click(object sender, EventArgs e)
    {
        string url = "~/App/Master/YigongSrhForm.aspx";

        //返回
        this.ViewState["Serch"] = "Serch";
        Context.Items["Serch"] = this.ViewState["Serch"];
        Context.Items["YIGONG_NAME"] = Convert.ToString(ViewState["YigongName"]);
        Context.Items["SEX"] = Convert.ToString(ViewState["Sex"]);
        Context.Items["SEND_DATE"] = Convert.ToString(ViewState["SendDate"]);
        Context.Items["SEND_DATE_TO"] = Convert.ToString(ViewState["SendDateTo"]);
        Context.Items["ParamPageIndex"] = Convert.ToString(ViewState["ParamPageIndex"]);
        ControllerUtils.Transfer(Context, url, true);

    }
}
